package com.datacloud.service.impl;

import java.util.List;

import com.datacloud.bean.*;
import com.datacloud.dao.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.datacloud.service.CarsManageService;

/**
 * Service Impl
 * Created by lv on 9/24/16.
 */
@Service
public class CarsManageServiceImpl implements CarsManageService {

    @Autowired
    VehiclesDao vehiclesDao;
    @Autowired
    RoutesDao routesDao;
    @Autowired
    TireBrandsDao tireBrandsDao;
    @Autowired
    TireModelsDao tireModelsDao;
    @Autowired
    TirePatternsDao tirePatternsDao;
    @Autowired
    TireStatussDao tireStatussDao;
    @Autowired
    TiresDao tiresDao;
    @Autowired
    TireEventsDao tireEventsDao;
    @Autowired
    ReasonDao reasonDao;
    /**
     * 获取未绑定车列表
     */
    @Override
    public List<VehiclesBean> getNoBindingCars() {
        return vehiclesDao.queryNoBinding();
    }
    /**
     * 进行车辆绑定
     */
    @Override
    public boolean binding(int carid,String epc){
    	int num = vehiclesDao.update(epc, carid);
    	if(num>=0){
    		return true;
    	}else{
    		return false;
    	}
    }
    /**
     * 获取所有线路
     */
    @Override
    public List<RoutesBean> findAllRoutes(){
    	return routesDao.findAllRoutes();
    }
    /**
     * 根据EPC获取车辆信息
     */
    @Override
    public VehiclesBean findVehiclesByEpc(String epc){
    	return vehiclesDao.findByEpc(epc);
    }
    /**
     * 获取所有轮胎品牌
     */
    @Override
    public List<TireBrandsBean> findAllTireBrands(){
    	return tireBrandsDao.findAllTireBrands();
    }
    /**
     * 获取所有轮胎规格
     */
    @Override
    public List<TireModelsBean> findAllTireModels(){
    	return tireModelsDao.findAllTireModels();
    }
    /**
     * 获取所有轮胎花纹
     */
    @Override
    public List<TirePatternsBean> findAllTirePatterns(){
    	return tirePatternsDao.findAllTirePatterns();
    }
    /**
     * 轮胎查询
     */
    @Override
    public TiresInfo findTiresInfo(String epc,String vehicleid,String wheelaliasid){
    	TiresBean tiresBean = tiresDao.findTiresBy(epc, vehicleid, wheelaliasid);
    	if(tiresBean!=null){
    		String tirebrands = tireBrandsDao.findTireBrandsName(tiresBean.getBrandid());
        	String tiremodels = tireModelsDao.findTireModelsName(tiresBean.getModelid());
        	String tirepatterns = tirePatternsDao.findTirePatternsName(tiresBean.getPatternid());
        	int depth = tiresBean.getDepth();
        	String tirestatuss = tireStatussDao.findTireStatussName(tiresBean.getStatusid());
        	int miles = tiresBean.getMiles();
        	TiresInfo info = new TiresInfo();
        	info.setDepth(depth);
        	info.setFanxin(0);
        	info.setMiles(miles);
        	info.setTirebrands(tirebrands);
        	info.setTiremodels(tiremodels);
        	info.setTirepatterns(tirepatterns);
        	info.setTirestatuss(tirestatuss);
        	info.setTiretype("");
        	return info;
    	}else{
    		return null;
    	}
    	
    }
    /**
     * 轮胎绑定
     */
    @Override
    public int bindingTire(TiresBean info){
    	
    	return tiresDao.bindingTire(info);
    }


    /*
     *轮胎检查
     */
    @Override
    public boolean tireCheck(int tireid,int depth,int step,int createid,String memo,int reasonid,int branchid){
        int num1= tiresDao.updateTireCheckinfo(tireid,depth);
        int num2= tireEventsDao.insertTireCheckInfo(tireid,step,createid,memo,reasonid,branchid);
        if(num1>=0&&num2>=0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 根据异常部位获取异常原因
     * @return
     */
    @Override
    public List<ReasonBeam>findReasonName(){
        return reasonDao.findReasonName();
    }
//在车检查的轮位操作
    public boolean carsTirescheck(int id,int depth){
        int num1=tiresDao.updateCarsTrieCheck(id,depth);
        int num2=tireEventsDao.insertCarsTireCheck(id);
        if(num1>=0&&num2>=0){
            return true;
        }else{
            return false;
        }
    }




    /***
     * 轮胎装车
     */

    @Override
    public boolean updateTire(int installid,int unloadid,int vehicleid,int wheelid,int stockid,TireEventsBean install,TireEventsBean unload){
        int updateTire = tiresDao.updateTire(installid,unloadid,vehicleid,wheelid,stockid);
        int installTire = tireEventsDao.installTire(install);
        int unloadTire = tireEventsDao.unloadTire(unload);
        if (updateTire>0 && installTire>0 && unloadTire>0){
            return true;
        }
        else {
            return false;
        }
    }




    //通过装上轮胎epc查找装上轮胎id
    @Override
    public int getInstallIdByEpc(String installepc){
        int installid = tiresDao.getInstallIdByEpc(installepc);
        return installid;
    }
    //通过卸下轮胎epc查找卸下轮胎id
    @Override
    public int getUnloadIdByEpc(String unloadepc){
        int unloadlid = tiresDao.getInstallIdByEpc(unloadepc);
        return unloadlid;
    }
    @Override
    public int getVehicleIdByEpc(String vehicleepc){
        int vehicleid = vehiclesDao.getVehicleIdByEpc(vehicleepc);
        return vehicleid;
    }


    //    通过轮位1轮胎的epc获取轮胎1id
    @Override
    public  int getTire1IdByEpc(String tire1epc){
        int tire1id=tiresDao.getTire1IdByEpc(tire1epc);
        return tire1id;
    };
    //    通过轮位2轮胎的epc获取轮胎2id
    @Override
    public int getTire2IdByEpc(String tire2epc){
        int tire2id=tiresDao.getTire2IdByEpc(tire2epc);
        return tire2id;
    };

    @Override
    public boolean exchangeWheels(int tire1id, int vehicleid,int wheel2id,int tire2id,int wheel1id,
                                  TireEventsBean install1,TireEventsBean unload1,
                                  TireEventsBean install2,TireEventsBean unload2){
        int exchangeWheel1 = tiresDao.exchangeWheel1(tire1id,vehicleid,wheel2id);
        int exchangeWheel2 = tiresDao.exchangewheel2(tire2id,vehicleid,wheel1id);
        int install1Tire = tireEventsDao.install1Tire(install1);
        int unload1Tire = tireEventsDao.unload1Tire(unload1);
        int install2Tire = tireEventsDao.install2Tire(install2);
        int unload2Tire = tireEventsDao.unload2Tire(unload2);
        if (exchangeWheel1>0 && exchangeWheel2>0 && install1Tire>0 && unload1Tire>0 && install2Tire>0 && unload2Tire>0){
            return true;
        }
        else {
            return false;
        }
    };

}
